#include "irm.h"

namespace ophies {

/////////////////////////////////////////////////////////////////////////
/// \brief Helper::getList
/// \param jsonStr
/// \return
/////////////////////////////////////////////////////////////////////////
QList<DataModel*>* Helper::listFromJsonStr(DataModelType type, const QString jsonStr, QObject* parent)
{
    Json::Value root;
	Json::Value children;
    Json::Reader reader;
	char* str;

    bool result = reader.parse(jsonStr.toStdString(), root);
    if(!result)
    {
//         report to the user the failure and their locations in the document.
        std::cout  << "Failed to parse configuration\n"
                   << reader.getFormattedErrorMessages().c_str() << std::endl;
        return NULL;
    }

    QList<DataModel*>* list = new QList<DataModel*>();

    Json::Value *temp;
    if(root.isArray())
    {
		switch(type) 
		{
		case DCM_STUDY:
			for(unsigned int i = 0; i < root.size(); i++)
			{
				temp = &root[i];
				DcmStudy *p = new DcmStudy(parent); // fixme
				p->fromJson(*temp);
				*list << (p);
			}
			break;
		case DCM_SERIES:
			for(unsigned int i = 0; i < root.size(); i++)
			{
				temp = &root[i];
				// FIXME
				/*children = &root[i]["dcm_instances"];
				if(children.isArray())
				{

				}*/
				DcmSeries *p = new DcmSeries(parent); // fixme
				p->fromJson(*temp);
				*list << (p);
			}
			break;
		case DCM_INSTANCE:
			for(unsigned int i = 0; i < root.size(); i++)
			{
				temp = &root[i];
				DcmInstance *p = new DcmInstance(parent); // fixme
				p->fromJson(*temp);
				*list << (p);
			}
			break;
		case DOCSET:
			for(unsigned int i = 0; i < root.size(); i++)
			{
				temp = &root[i];
				Docset *p = new Docset(parent); // fixme
				p->fromJson(*temp);
				*list << (p);
			}
			break;
		case PATIENT:
			for(unsigned int i = 0; i < root.size(); i++)
			{
				temp = &root[i];
				Patient *p = new Patient(parent); // fixme
				p->fromJson(*temp);
				*list << (p);
			}
			break;
		default:
			for(unsigned int i = 0; i < root.size(); i++)
			{
				temp = &root[i];
				DataModel *p = new DataModel(UNKNOWN, parent); // fixme
				p->fromJson(*temp);
				*list << (p);
			}
			break;
		}

       
    } else
    {

    }

    return list;
}

/////////////////////////////////////////////////////////////////////////
/// \brief Helper::objectFromJsonStr
/// \param type
/// \param jsonStr
/// \return
/////////////////////////////////////////////////////////////////////////
DataModel* Helper::objectFromJsonStr(DataModelType type, const QString jsonStr, QObject* parent)
{

    DataModel* d;
	switch(type) 
	{
	case DCM_STUDY:
		d = new DcmStudy(parent);
		break;
	case DCM_SERIES:
		d = new DcmSeries(parent);
		break;
	case DCM_INSTANCE:
		d = new DcmInstance(parent);
		break;
	case DOCSET:
		d = new Docset(parent);
		break;
	case PATIENT:
		d = new Patient(parent);
		break;
	default:
		d = new DataModel(UNKNOWN, parent);
		break;
	}

	d->fromJson(jsonStr);
    return d;
}


}
